<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\warning\controller\ajax;


class Present {
	
	/**
	 * 柱状图数据(各企业预警系数)
	 */
	public function getBarData(){
		$warning = model('warning')->with('enterprise')->select();
		$data = [];
		$barData['enterpriseList' ] = [];
		$barData['warningData'] = [];
		$barData['riskDegree'] = [];
		foreach($warning as $item){
			$barData['enterpriseList'][] = $item['enterprise']['shortname'];
			$barData['warningData'][] = $item['generalValue'];
		}
		$riskDegree = model('Config')->where('groupName','risk_degree')->order('name desc')->select();
		foreach($riskDegree as $item){
			list($min,$max,$color) = explode(',',$item['value']);
			$degree['min'] = (int)$min;
			$degree['max'] = (int)$max;
			$degree['label'] = $item['title'];
			$degree['color'] = $color;
			$barData['riskDegree'][] = $degree;
		}
		return json(['code'=>1,'msg'=>'预警分数获取成功','data'=>$barData]);
	}
	
	/**
	 * 雷达图数据(片区预警系数)
	 */
	public function getRadarData(){
		$warning = model('warning')
					->alias('w')
					->join('enterprise e','e.id=w.enterpriseId')
					->join('basicinfo b','b.sysCode=1 AND b.subsystemNo=e.positionId')
					->group('positionId')
					->field('avg(techniqueValue) as techniqueValue,
							avg(equipmentValue) as equipmentValue, 
							avg(workingValue) as workingValue,
							avg(behaviorValue) as behaviorValue,
							avg(manageValue) as manageValue,
							positionId,subsystemName as positionName')
					->select();
		$radarData['legend'] = [];
		$radarData['indicator'] = [
				['name'=>'生产工艺','max'=>20],
				['name'=>'设备设施','max'=>20],
				['name'=>'工作环境','max'=>10],
				['name'=>'人员行为','max'=>20],
				['name'=>'管理体系','max'=>50],
		];
		$radarData['warningData'] = [];
		$technique = 0;
		$equipment = 0;
		$working = 0;
		$behavior = 0;
		$manage = 0;
		foreach ($warning as $item){
			$radarData['legend'][] = $item['positionName'];
			$radarData['warningData'][] = [
											'value'=>[
												(int)$item['techniqueValue'],
												(int)$item['equipmentValue'],
												(int)$item['workingValue'],
												(int)$item['behaviorValue'],
												(int)$item['manageValue'],
											],
											'name'=>$item['positionName'],
										];
			$technique += $item['techniqueValue'];
			$equipment += $item['equipmentValue'];
			$working += $item['workingValue'];
			$behavior += $item['behaviorValue'];
			$manage += $item['manageValue'];
		}
		$num_legend = count($radarData['legend']);
		$radarData['warningData'][] = ['value'=>[
											(int)($technique/$num_legend),
											(int)($equipment/$num_legend),
											(int)($working/$num_legend),
											(int)($behavior/$num_legend),
											(int)($manage/$num_legend)
										],
										'name'=>'建德园区'];
		$radarData['legend'][] = '建德园区';
		return json(['code'=>1,'msg'=>'预警分数获取成功','data'=>$radarData]);
	}
	
	/**
	 * 雷达表数据(企业预警参数)
	 */
	public function getRadarListData(){
		$warning = model('basic/enterprise')->with('warning')->select();
		$riskDegree = model('Config')->where('groupName','risk_degree')->order('name desc')->column('value','name');
		$indicator = [
				['name'=>'生产工艺','max'=>20],
				['name'=>'设备设施','max'=>20],
				['name'=>'工作环境','max'=>10],
				['name'=>'人员行为','max'=>20],
				['name'=>'管理体系','max'=>50],
		];
		$radarListData = [];
		foreach($warning as $item){
			$radarData = [
								'title' => $item['shortname'],
								'indicator' => $indicator,
								'id' => $item['id'],
								'color' => explode(",",$riskDegree[$item['warning']['riskDegree']])[2],
			];
			if(!empty($item['warning'])){
				$radarData['data'] =   [
										'value'=>[
											(int)$item['warning']['techniqueValue'],
											(int)$item['warning']['equipmentValue'],
											(int)$item['warning']['workingValue'],
											(int)$item['warning']['behaviorValue'],
											(int)$item['warning']['manageValue'],
										],
										
				];
			}
			$radarListData[] = $radarData;
		}
		return json(['code'=>1,'msg'=>'预警分数获取成功','data'=>$radarListData]);
	}
	
	/**
	 * 园区预警
	 * @param string $scope
	 */
	public function getAvgRadarData($enterpriseId = null){
		$where = [];
		if($enterpriseId){
			$where['enterpriseId'] = $enterpriseId;
		}
		$riskDegree = model('Config')->where('groupName','risk_degree')->order('name desc')->select();
		$warning = model('warning')->where($where)->field('avg(techniqueValue) as techniqueValue,
							avg(equipmentValue) as equipmentValue,
							avg(workingValue) as workingValue,
							avg(behaviorValue) as behaviorValue,
							avg(manageValue) as manageValue,
							avg(generalValue) as generalValue')->find();
		$indicator = [
				['name'=>'生产工艺','max'=>20],
				['name'=>'设备设施','max'=>20],
				['name'=>'工作环境','max'=>10],
				['name'=>'人员行为','max'=>20],
				['name'=>'管理体系','max'=>50],
		];
		foreach($riskDegree as $item){
			list($min,$max,$color) = explode(',',$item['value']);
			if ($warning['generalValue'] >= $min && $warning['generalValue'] < $max){
				break;	
			}
		}
		$radarData = [
				'title' => $enterpriseId?model('basic/enterprise')->get($enterpriseId)->shortname:'园区',
				'indicator' => $indicator,
				'color' => $color,
				'data' => ['value'=>[
						(int)$warning['techniqueValue'],
						(int)$warning['equipmentValue'],
						(int)$warning['workingValue'],
						(int)$warning['behaviorValue'],
						(int)$warning['manageValue'],
				]],
		];
		return json(['code'=>1,'msg'=>'预警分数获取成功','data'=>$radarData]);
	}
	
	/**
	 * 
	 * @param string $enterpriseId
	 */
	public function getDegreePieData($enterpriseId = null){
		$riskDegree = model('Config')->where('groupName','risk_degree')->order('name desc')->column('title','name');
		$pieData = [];
		if($enterpriseId){
			$degree = model('Warninghistory')
					   ->where('enterpriseId',$enterpriseId)
					   ->group('riskDegree')
					   ->column('count(id) as number','riskDegree');
			$pieData['title'] = model('basic/enterprise')->get($enterpriseId)->shortname;
		}else{
			$degree = model('warning')
						->group('riskDegree')
						->column('count(id) as number','riskDegree');
			$pieData['title'] = '园区';
		}
		foreach ($riskDegree as  $key=>$item){
			$pieData['legend'][] = $item;
			if(isset($degree[$key])){
				$pieData['series'][] = ['value'=>$degree[$key],'name'=>$item];
			}else{
				$pieData['series'][] = ['value'=>0,'name'=>$item];
			}
		}
		return json(['code'=>1,'msg'=>'获取成功','data'=>$pieData]);
		
	}
}